package script.esxi;

import cn.com.betasoft.dsat.probe.groovy.SimException;
import cn.com.betasoft.dsat.probe.groovy.util.*;
/**
原生变量：g_col_pars g_col_other g_rets
原生变量：g_res(g_shc, g_dbcon)
样例
~ # ps -PTjsu
WID         CID     World Name               GID    PCID    State  Wait    CPU     Time         Command
57389       57309   vmx-vthread-4:centos_72  57309  2163    WAIT   UFUTEX  0-3     0.84         /bin/vmx
75827       75711   vmx-vthread-4:centos5.3  75711  2163    WAIT   UFUTEX  0-3     0.82         /bin/vmx
75828       75711   vmx-mks:centos5.3        75711  2163    WAIT   UPOL    0-3     43.744343    /bin/vmx
75829       75711   vmx-vcpu-0:centos5.3     75711  2163    WAIT   IDLE    0-3     44207.412034 /bin/vmx
57447       57309   vmx-mks:centos_72        57309  2163    WAIT   UPOL    0-3     38.290582    /bin/vmx
57448       57309   vmx-vcpu-0:centos_72     57309  2163    RUN    NONE    0-3     44121.497981 /bin/vmx
2163        2163    init                     2163   0       WAIT   UWAIT   0-3     0.34762      /sbin/init
3311732     3076    vpxa-worker              3076   3069    WAIT   UFUTEX  0-3     664.78353    /usr/lib/vmware/vpxa/bin/vpxa
30361726    3076    vpxa-worker              3076   3069    WAIT   UFUTEX  0-3     398.898300   /usr/lib/vmware/vpxa/bin/vpxa
3311778     3076    vpxa-worker              3076   3069    WAIT   UFUTEX  0-3     673.931386   /usr/lib/vmware/vpxa/bin/vpxa

*/
def json = [];
//获取数据
def String ret = g_shc.collect('''ps -PTjsu''');
if (ret.contains("not found")){
	throw new SimException(8019,"ESXi.OSProcess:没有找到命令.");
}
if (ret.contains("permission denied")){
	throw new SimException(8020,"ESXi.OSProcess:没有权限执行命令.");
}
def matched = false;
ret.split("\n").each{
	matched = true;
	def matcher = (it =~ /(\d+)\s+(\d+)\s+(\S+)\s+(\d+)\s+(\d+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\d+\.\d+)\s+(\S+)/);
	if (matcher.matches()){
		def process =['CMD':'','UID':'','PID':-1,'PPID':-1,'CPU':-1.0f,'MEMRatio':-1.0f,'MEM':-1,'SZ':-1,'TTY':'','ElapsedTime':'','CPUTime':'','UserGroup':'','BeginTime':'','Status':''];
		process['CMD'] = matcher[0][3];//进程名称 World Name
		process['PID'] = matcher[0][1];//进程ID WID
		process['ElapsedTime'] = matcher[0][9].toString();//持续运行时间 Time
		process['UserGroup'] = matcher[0][4].toString();//进程用户组 GID 
		process['Status'] = matcher[0][6];//进程状态 State
		process['PPID'] = matcher[0][2];//父进程ID CID
		json.add(process);
	}
};
if(!matched){
	throw new SimException(8021,"ESXi.OSProcess:返回数据不匹配.");
}
//序列化返回
g_rets = json;